package com.landeqing.controller;

import com.landeqing.model.BanJi;
import com.landeqing.model.Student;
import com.landeqing.service.BanJiService;
import com.landeqing.service.StudentService;
import com.landeqing.vo.StudentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

@Controller
@RequestMapping("student")
public class StudentController {
    @Autowired
    private StudentService studentService;
    @Autowired
    private BanJiService banJiService;
    @RequestMapping("save")
    public ModelAndView  save(@RequestParam(value = "id" ,required = false) Integer id,
                                     @RequestParam(value = "name" ,required = false) String name,
                                     @RequestParam(value = "cid" ,required = false)Integer cid){

        ModelAndView mav=new ModelAndView("index");
        Student student=new Student();
        BanJi c=new BanJi();
        //student.setId(id);
        student.setName(name);
        student.setCid(cid);
        boolean result=studentService.insert(student);
        System.out.println(result);
        return mav;
    }
    @RequestMapping("toAdd")
    public  ModelAndView toAdd(){
        ModelAndView mav=new ModelAndView("index");
        return mav;
    }
    /**
     * 查询学生
     */
    @RequestMapping("list")
    public  ModelAndView findAll(HttpServletRequest request){
//        int currentPage=0;
//        String pageNo =request.getParameter("pNo");
//
//        currentPage=(Integer.parseInt(pageNo)- 1) * pageSize;
//        map.put(pageNo, currentPage);
//        Integer pSize=2;
//        map.put(pageSize,pSize);
        int pageNo = 1;
        int pageSize = 2;
        if (request.getParameter("page")!=null&&Integer.parseInt(request.getParameter("page"))>=1){
            pageNo=Integer.parseInt(request.getParameter("page"));
        }
        List<Student> list=studentService.findAll(pageNo,pageSize);
        int count=studentService.getTotalCount();
        int TotalPages;
        if (count%pageSize==0){
            TotalPages=count/pageSize;
        }else{
            TotalPages=count/pageSize+1;
        }
        List<StudentVo> voList=new ArrayList<StudentVo>();
        for (Student s:list) {
            BanJi banJi=banJiService.getBanJiId(s.getCid());
                StudentVo vo=new StudentVo();
                vo.setId(s.getId());
                vo.setName(s.getName());
                vo.setCid(s.getCid());
                vo.setClassName(banJi.getClassName());
                vo.setDate(new Date());
                vo.setMoney(12.369);
                voList.add(vo);
        }
        ModelAndView mav=new ModelAndView("/student/list");
        mav.addObject("list",voList);
        mav.addObject("pageNo",pageNo);
        mav.addObject("TotalPages",TotalPages);
        return mav;
    }
    @RequestMapping("getById")
    public ModelAndView getById(Integer id){
        ModelAndView mav=new ModelAndView("/student/student");
        Student student=studentService.getById(id);
        mav.addObject("student",student);
        return  mav;
    }
}
